package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.OrderAttachment;
import com.ruoyi.system.domain.RxOrderFollow;
import com.ruoyi.system.domain.vo.RxOrderFollowVo;
import com.ruoyi.system.mapper.OrderAttachmentMapper;
import com.ruoyi.system.mapper.RxOrderFollowMapper;
import com.ruoyi.system.service.IRxOrderFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单跟进记录Controller
 *
 * @author hjf
 * @date 2025-03-05
 */
@RestController
@RequestMapping("/hmk/follow")
public class RxOrderFollowController extends BaseController {
    @Autowired
    private IRxOrderFollowService rxOrderFollowService;

    @Autowired
    private RxOrderFollowMapper rxOrderFollowMapper;

    @Autowired
    private OrderAttachmentMapper oAM;

    /**
     * 查询订单跟进记录列表
     */
    @PreAuthorize("@ss.hasPermi('hmk:follow:list')")
    @GetMapping("/list")
    public TableDataInfo list(RxOrderFollow rxOrderFollow) {
        startPage();
        List<RxOrderFollow> list = rxOrderFollowMapper.selectRxOrderFollowList(rxOrderFollow);
        return getDataTable(list);
    }


    @PostMapping(value = "/getOrderFollowById")
    public AjaxResult getOrderFollowById(@RequestBody RxOrderFollowVo vo) {
        List<RxOrderFollowVo> list = rxOrderFollowMapper.getOrderFollowById(vo.getId());
        if (list.size() > 0) {
            for (RxOrderFollowVo ofv : list) {
                if (ofv != null && StringUtils.isNotEmpty(ofv.getTitle())) {
                    String title = ofv.getTitle();
                    if (title.contains("交易失败") || title.contains("订单废弃")) {
                        OrderAttachment orderAttachment = new OrderAttachment();
                        orderAttachment.setOrderFollow(ofv.getId());
                        List<OrderAttachment> orderAttachments = oAM.selectOrderAttachmentList(orderAttachment);
                        for (OrderAttachment oa : orderAttachments) {
                            if ("1".equals(oa.getFileType())) {
                                ofv.setPictureTag(true);
                                continue;
                            }
                            if ("2".equals(oa.getFileType())) {
                                ofv.setAudioTag(true);
                            }
                        }
                    }
                }
            }
        }
        return success(list);
    }


    /**
     * 导出订单跟进记录列表
     */
    @PreAuthorize("@ss.hasPermi('hmk:follow:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, RxOrderFollow rxOrderFollow) {
        List<RxOrderFollow> list = rxOrderFollowService.selectRxOrderFollowList(rxOrderFollow);
        ExcelUtil<RxOrderFollow> util = new ExcelUtil<RxOrderFollow>(RxOrderFollow.class);
        util.exportExcel(response, list, "订单跟进记录数据");
    }

    /**
     * 获取订单跟进记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('hmk:follow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(rxOrderFollowService.selectRxOrderFollowById(id));
    }

    /**
     * 新增订单跟进记录
     */
    @PreAuthorize("@ss.hasPermi('hmk:follow:add')")
    @PostMapping
    public AjaxResult add(@RequestBody RxOrderFollow rxOrderFollow) {
        return toAjax(rxOrderFollowService.insertRxOrderFollow(rxOrderFollow));
    }

    /**
     * 修改订单跟进记录
     */
    @PreAuthorize("@ss.hasPermi('hmk:follow:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody RxOrderFollow rxOrderFollow) {
        return toAjax(rxOrderFollowService.updateRxOrderFollow(rxOrderFollow));
    }

    /**
     * 删除订单跟进记录
     */
    @PreAuthorize("@ss.hasPermi('hmk:follow:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(rxOrderFollowService.deleteRxOrderFollowByIds(ids));
    }
}
